/**
 * Created by IntelliJ IDEA.
 * User: Tomek
 * Date: 30.06.12
 * Time: 15:46
 * To change this template use File | Settings | File Templates.
 */
package AS3 {
import AS3.constants.Coefficients;
import AS3.constants.ConvertRatio;
import AS3.constants.ConvertRatio;
import AS3.constants.ConvertRatio;
import AS3.constants.ConvertRatio;
import AS3.constants.ConvertRatio;
import AS3.constants.ConvertRatio;
import AS3.constants.combo.FossilUnits;

public class EmissionHelper {
    public function EmissionHelper() {
    }

    public static function getSumOfAllEmissions(emissions:CountryEmission):Number {
        return calcReferenceSum(emissions);
    }

    public static function getNumberOfTreesEquivalent(emissions:CountryEmission):Number {
        return calcReferenceSum(emissions) * Coefficients.TREES_PER_TONNE_CO2;
    }

    public static function getNumberOfPeopleEquivalent(emissions:CountryEmission):Number {
        return calcReferenceSum(emissions) * emissions.getCO2KWhRatio();
    }

    public static function getAverageEmissionRatio(emissions:CountryEmission):Number {
        return calcReferenceSum(emissions) * emissions.averageEmission;
    }

    public static function convertToUnits(emissions:CountryEmission,unit:int): ConvertedEmission {
        var converted:ConvertedEmission = new ConvertedEmission();

        var co2PerKWh:Number = emissions.getCO2KWhRatio();

        converted.general = convertValue(unit,co2PerKWh,emissions.general);
        converted.deforestation = convertValue(unit,co2PerKWh,emissions.deforestation);
        converted.electricity = convertValue(unit,co2PerKWh,emissions.electricity);
        converted.consumption = convertValue(unit,co2PerKWh,emissions.consumption);
        converted.food = convertValue(unit,co2PerKWh,emissions.food);
        converted.flights = convertValue(unit,co2PerKWh,emissions.flights);
        converted.publicTransport = convertValue(unit,co2PerKWh,emissions.publicTransport);
        converted.carProduction = convertValue(unit,co2PerKWh,emissions.carProduction);
        converted.fuelBurnings = convertValue(unit,co2PerKWh,emissions.fuelBurnings);
        converted.airCondition = convertValue(unit,co2PerKWh,emissions.airCondition);
        converted.warmWater = convertValue(unit,co2PerKWh,emissions.warmWater);
        converted.heating = convertValue(unit,co2PerKWh,emissions.heating);
        converted.maintenance = convertValue(unit,co2PerKWh,emissions.maintenance);

        converted.averageValue = convertValue(unit,co2PerKWh,emissions.averageEmission);
        converted.exportValue = convertValue(unit,co2PerKWh,emissions.exportEmission);

        converted.emissionEU = convertValue(unit,co2PerKWh,Coefficients.EMISSION_EU);
        converted.emissionUS = convertValue(unit,co2PerKWh,Coefficients.EMISSION_US);
        converted.emissionCH = convertValue(unit,co2PerKWh,Coefficients.EMISSION_CHINA);
        converted.emissionIndia = convertValue(unit,co2PerKWh,Coefficients.EMISSION_INDIA);

        converted.totalValue = sumAllEmissions(converted);
        converted.originalValue = calcReferenceSum(emissions);

        converted.coalEmission = converted.originalValue - emissions.fuelBurnings;
        converted.gasEmission = converted.originalValue - emissions.fuelBurnings;
        converted.oilEmission = converted.gasEmission + emissions.fuelBurnings;


       /*
        converted.coalConsumption = converted.originalSumEmission *  emissions.coalTonnePerCO2Tonne * 1000;
        converted.oilConsumption = converted.originalSumEmission *  emissions.oilTonnePerCO2Tonne * 1000;
        converted.gasConsumption = converted.originalSumEmission * emissions.gasM3PerCO2Tonne / 1.25;
       */

        converted.coalConsumption = emissions.fossilConsumption.coal;
        converted.oilConsumption = emissions.fossilConsumption.oil;
        converted.gasConsumption = emissions.fossilConsumption.gas;

        return converted;
    }


    private static function sumAllEmissions(converted:ConvertedEmission):Number {
        return converted.general + converted.deforestation + converted.electricity +
               converted.consumption + converted.food + converted.flights +
               converted.publicTransport + converted.carProduction + converted.fuelBurnings +
               converted.airCondition + converted.warmWater + converted.heating +
               converted.maintenance;

    }

    private static function calcReferenceSum(original:CountryEmission):Number {
        return original.general + original.deforestation + original.electricity +
               original.consumption + original.food + original.flights +
               original.publicTransport + original.carProduction + original.fuelBurnings +
               original.airCondition + original.warmWater + original.heating +
               original.maintenance;

    }


    private static function convertValue(unit:int,co2PerKWh:Number,emissions:Number):Number {
        var result:Number = new Number(emissions);

        switch (unit) {
            case FossilUnits.KG_CO2_PER_DAY: result = ConvertRatio.TON_CO2_YEAR_PER_CO2_DAY * result;break;
            case FossilUnits.KG_CO2_PER_MONTH: result = ConvertRatio.TON_CO2_YEAR_PER_CO2_MONTH * result;break;
            case FossilUnits.MWh_PER_YEAR: result = co2PerKWh * ConvertRatio.KWh_DAY_PER_MWh_YEAR * result;  break;
            case FossilUnits.MJ_PER_YEAR: result = co2PerKWh * ConvertRatio.KWh_DAY_PER_MJ_DAY * result;  break;
            case FossilUnits.GJ_PER_YEAR: result = co2PerKWh * ConvertRatio.KWh_DAY_PER_GJ_YEAR * result;  break;
            case FossilUnits.TOE_PER_YEAR: result = co2PerKWh * ConvertRatio.KWh_DAY_PER_TON_YEAR * result;  break;
            case FossilUnits.KCAL_PER_DAY: result = co2PerKWh * ConvertRatio.KWh_DAY_PER_KCAL_DAY * result;  break;
            case FossilUnits.BTU_PER_DAY: result = co2PerKWh * ConvertRatio.KWh_DAY_PER_BTU_DAY * result;  break;
            case FossilUnits.MLN_BTU_PER_YEAR: result = co2PerKWh * ConvertRatio.KWh_DAY_PER_MLN_BTU_YEAR * result;  break;
            case FossilUnits.KWh_PER_DAY: result = co2PerKWh * result;  break;
        }
        return result;
    }

}
}
